package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.info.Accountinfo;
import com.woniuxy.model.Account;
import com.woniuxy.model.AccountRule;
import com.woniuxy.model.Rule;
import com.woniuxy.service.AccountRuleService;
import com.woniuxy.service.AccountService;
import com.woniuxy.service.RuleService;
import com.woniuxy.util.WoniuCopyUtil;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccountDetailsServiceImpl implements UserDetailsService {
    @Resource
    private AccountService accountService;
    @Resource
    private AccountRuleService accountRuleService;
    @Resource
    private RuleService ruleService;



    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("查询的姓名未："+s);
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name",s);
        Account user = accountService.getOne(queryWrapper);
        if(user==null) throw new UsernameNotFoundException("用户名不存在");

//        QueryWrapper<AccountRule> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.eq("account_id",user.getAccountId());
//        List<Integer> ruleIds = accountRuleService.list(queryWrapper1).stream().map(rm -> rm.getRuleId()).collect(Collectors.toList());
//        QueryWrapper<Rule> queryWrapper2 = new QueryWrapper<>();
//        queryWrapper2.in("rule_id",ruleIds);
//        List<GrantedAuthority> rules = ruleService.list(queryWrapper2).stream().map(rm -> {
//            return  new GrantedAuthority() {
//                @Override
//                public String getAuthority() {
//                    return rm.getRuleIndentify();
//                }
//            };
//        }).collect(Collectors.toList());
        Accountinfo accountinfo = WoniuCopyUtil.copyOne(user, Accountinfo.class);
//        accountinfo.setRules(rules);
        return accountinfo;

    }


}
